// pages/refuelingStation/refuelingStation.js
var that,
  api = new (require('../../utils/api.js')),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refulinglist:[
    ],
    lng:'',
    lat:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.getdate()
  },
  getdate(){
    let data={}
    wx.getLocation({
      success: res => {
        console.log(res,'rwesres');
        // wx.showLoading({
        //   title: '加载中',
        // })
        that.setData({lng:res.longitude,lat:res.latitude})
        data={lat:res.latitude,lng:res.longitude}
        that.systemstation(data)
      },
      fail:res=>{
        data={lng:'',lat:''}
        that.getlist()
        wx.getSetting({
          success:res=>{
            if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
    
              wx.showModal({
                content: '加油站需要获取地址位置权限，是否前往开启',
                confirmText:'前往开启',
                success:res=>{
                  if(res.confirm){
                    wx.openSetting({
                      success: res => {
                        if (res.authSetting['scope.userLocation']) {
                          // 授权成功，重新定位
                          wx.getLocation({
                            success: res => {
                              // wx.showLoading({
                              //   title: '加载中',
                              // })
                              that.setData({lat:res.latitude,lng:res.longitude})
                              data={lng:res.longitude,lat:res.latitude}
                              that.systemstation(data)
                            },fail:res=>{
                              data={lng:'',lat:''}
                              that.systemstation(data)
                            }
                          });
                        } else {
                          // 没有允许定位权限
                          data={lng:'',lat:''}
                          that.systemstation(data)
                        }
                      },fail:res=>{
                        data={lng:'',lat:''}
                        that.systemstation(data)
                      }
                    });
                  }else{
                    data={lng:'',lat:''}
                    that.systemstation(data)
                  }
                },fail:res=>{
                  data={lng:'',lat:''}
                  that.systemstation(data)
                }
              })
            }else{
               wx.getLocation({
                     success:res=>{
                      that.setData({lat:res.latitude,lng:res.longitude})
                      data={lat:res.latitude,lng:res.longitude}
                      that.systemstation(data)
                     }
                 })
            }
          },fail:res=>{
            data={lng:'',lat:''}
            that.systemstation(data)
          }
        }) 
      }
      
    })
  },
  getlist(data){
    console.log(data,'systemstation--datadatadata');
    
    api.systemstation(data,res1=>{
      wx.hideLoading()
      if(res1.data.code==0){
        let refulinglist=[]
        if(res1.data.entity.data.length>0){
          res1.data.entity.data.forEach(x=>{
            if(parseInt(x.distance)>1000){
              x.distance=(x.distance/1000).toFixed(2)+'km'
            }else{
              x.distance=x.distance+'m'
            }
            refulinglist.push(x)
          })
        }
        that.setData({refulinglist})
      }
    })
  },
  systemstation(data){
    wx.showLoading({
      title: '加载中',
    })
    api.systemstation(data,res1=>{
      wx.hideLoading()
      if(res1.data.code==0){
        // if(res1.data.entity.data.length==1){
        //   wx.navigateTo({
        //     url: '../invoice/invoice?id='+res1.data.entity.data[0].id,
        //   })
        // }
        let refulinglist=[],distanceid=0
        if(res1.data.entity.data.length>0){
          res1.data.entity.data.forEach(x=>{
            if(parseInt(x.distance)<80){
              distanceid=x.id
            }
            if(parseInt(x.distance)>1000){
              x.distance=(x.distance/1000).toFixed(2)+'km'
            }else{
              x.distance=x.distance+'m'
            }
            refulinglist.push(x)
          })
        }
        that.setData({refulinglist},()=>{
          if(res1.data.entity.data.length==1){
            wx.navigateTo({
              url: '../invoice/invoice?id='+res1.data.entity.data[0].id,
            })
          }else{
            if(res1.data.entity.data.length>0){
              // let distanceid=0
              // res1.data.entity.data.forEach(x=>{
              //   if(parseInt(x.distance)<80){
              //     distanceid=x.id
                  
              //   }
              // })
              if(distanceid>0){
                wx.navigateTo({
                  url: '../invoice/invoice?id='+distanceid,
                })
              }
            }
          }
        })
      }else{
        wx.showModal({
          content:res1.data.message,
          showCancel:!1,
          success(){
            wx.navigateBack()
          }
        })
      }
      // if(res1.data.code==0){
      //   let refulinglist=res1.data.entity.data
      //   refulinglist.forEach(x=>{
      //     if(parseInt(x.distance)>1000){
      //       x.distance=(x.distance/1000).toFixed(2)+'km'
      //     }else{
      //       x.distance=x.distance+'m'
      //     }
      //   })
      //   that.setData({refulinglist})
      // }
    })
  },
 tofuelcharging(e){
  //  console.log(e.currentTarget.dataset.id,'e.currentTarget.dataset.id');
   
   wx.navigateTo({
     url: '../invoice/invoice?id='+e.currentTarget.dataset.id,
   })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})